package sandboxGUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class PasswordGUI {
	
	JFrame framePasswordWindow;
	private JPanel passwdPanel;
	
	//Password GUI Variables
	private JTextField usernameField;
	private JLabel lblPassword;
	JLabel lblUsername;
	JButton btnPasswdSubmit;
	JButton btnExitPasswodGUI;
	JButton btnOtherLogin;
	JLabel lblPasswordStatus;
	private JPasswordField passwordField;
	private int passwordTrials = 0;
	
		
	
	SandboxWindowGUI masterWindow;
	


	public PasswordGUI(SandboxWindowGUI window) {
		masterWindow = window;
		buildPasswordGUI();
	}
	
	

	
	public void buildPasswordGUI() {
						
		
		//Setup the JFrame
		framePasswordWindow = new JFrame();
		framePasswordWindow.setSize(310, 420);//use this instead of set bounds in order to avoid having to set the starting x and y coordinates
//framePasswordWindow.setSize(1024, 768);//DEBUG: add this line as DEBUG in order to easier work on the design for mainGUI. Comment out in final build
		framePasswordWindow.setTitle("Login");//Set frame title
		framePasswordWindow.setLocationRelativeTo(null);//allows the frame to be in the center by default
		framePasswordWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		framePasswordWindow.setVisible(true);

		//Setup the JPanel
		passwdPanel = new JPanel();
		framePasswordWindow.getContentPane().add(passwdPanel, BorderLayout.CENTER);
		passwdPanel.setLayout(null);
		
		//Setup the components
		lblUsername = new JLabel("Username");
		lblUsername.setBounds(64, 28, 125, 14);
		passwdPanel.add(lblUsername);
		
		usernameField = new JTextField();
		usernameField.setBounds(64, 53, 169, 20);
		passwdPanel.add(usernameField);
		usernameField.setColumns(10);
		
		lblPassword = new JLabel("Password");
		lblPassword.setBounds(64, 100, 125, 14);
		passwdPanel.add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(64, 125, 169, 20);
		passwdPanel.add(passwordField);
		
		btnPasswdSubmit = new JButton("Submit");

		btnPasswdSubmit.setBounds(88, 203, 122, 23);
		passwdPanel.add(btnPasswdSubmit);
		
		btnOtherLogin = new JButton("Other Login");
		btnOtherLogin.setBounds(88, 251, 122, 23);
		passwdPanel.add(btnOtherLogin);
		
		lblPasswordStatus = new JLabel("");
		lblPasswordStatus.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPasswordStatus.setBounds(10, 144, 274, 14);
		passwdPanel.add(lblPasswordStatus);
		
		btnExitPasswodGUI = new JButton("Exit");
		btnExitPasswodGUI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exitProgram();
			}
		});
		btnExitPasswodGUI.setBounds(222, 327, 62, 23);
		passwdPanel.add(btnExitPasswodGUI);
		
		btnPasswdSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				checkPassword();
			}//end actionPerformed
		});//end addActionListener

		
	}//buildPasswordGUI
	
	private void exitProgram() {
		//Exit the program
		System.exit(0);
	}
	
	private void checkPassword() {
		//test password conditions
	
		char [] passwdCharArray;
		passwdCharArray = passwordField.getPassword();
		String passwdString = new String(passwdCharArray);
		if( "test".equals(passwdString) )//check the password
		{
			passwordPassed();
		}//end if
		else
		{
			passwordFailed();
		}//end else

	}//end checkPassword()
	

	
	private void passwordPassed() {
		//action to perform when the password has passed the test
		resetPasswordGUI();//reset the password GUI for next time
		//Create Main GUI here
		System.out.println("Valid Login");//debug
		hideFrame();//hide password window
		masterWindow.seeFrame();//show master window

	}//end passwordPassed()
	private void passwordFailed() {
		masterWindow.hideFrame();
		
		//action to perform when the password has failed the test
		clearPasswordGUI();//clear the password fields
		passwordTrials++;//increment password trials
		if(passwordTrials == 3)//allow user three tries
		{
			disablePasswordGUI();//lock out the system
//update to throttle... also to add
			lblPasswordStatus.setText("Incorrect - Too many tries. System is locked!");//display the status message
		}//end if
		else
		{
			lblPasswordStatus.setText("Incorrect - Please try again!");//display the status message
		}//end else
	
	}//end passwordFailed()
	
	private void clearPasswordGUI() {
		//clear the password fields
		passwordField.setText(null);//clear the password displayed
		usernameField.setText(null);//clear the username displayed
		usernameField.requestFocus();//return focus to the username field
		lblPasswordStatus.setText("");		
	}//end clearPasswordGUI
	
	private void disablePasswordGUI() {
		//disable the password GUI elements from use to lock out the system login
		usernameField.setEnabled(false);
		passwordField.setEnabled(false);
		btnPasswdSubmit.setEnabled(false);
		btnOtherLogin.setEnabled(false);
	}//end disablePasswordGUI
	
	
	public void resetPasswordGUI() {
		//resets password GUI window for next time it is used
		passwordTrials = 0;//clear the password trials counter
		clearPasswordGUI();//clear the password fields
	}//end resetPasswordGUI
	
	public void seeFrame() {
		framePasswordWindow.setVisible(true);	
	}//seeFrame()

	public void hideFrame() {
		framePasswordWindow.setVisible(false);	
	}//seeFrame()
	
	
	
}
